package com.da.demo.security.service;

import com.da.demo.security.entity.DailyUsers;
import com.da.demo.security.entity.vo.DailyUsersVO;
import com.da.demo.security.mapper.DailyUsersMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-02-24
 * @Version: 1.0
 */
@Service("userDetailsServiceImpl")
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final DailyUsersMapper dailyUsersMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        DailyUsers dailyUsers = dailyUsersMapper.queryByUserName(username);
        DailyUsersVO dailyUsersVO = new DailyUsersVO();
        dailyUsersVO.setId(dailyUsers.getId());
        dailyUsersVO.setUsername(dailyUsers.getUsername());
        dailyUsersVO.setPassword(dailyUsers.getPassword());
        dailyUsersVO.setStatus(dailyUsers.getStatus());
        dailyUsersVO.setEmail(dailyUsers.getEmail());
        dailyUsersVO.setPhoneNumber(dailyUsers.getPhoneNumber());
        dailyUsersVO.setAvatar(dailyUsers.getAvatar());
        if (dailyUsers == null) {
            throw new UsernameNotFoundException("登录账号不存在");
        }
        return dailyUsersVO;
    }
}
